<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>js时钟</title>
	<script type="text/javascript">
		window.onload = function(){
			var oDiv = document.getElementById('div1');

			function timego(){
				// 实例化一个对象
				var now = new Date();
				// 弹出一个标准时间

				var year = now.getFullYear();

				// 从第一月算起,是0
				var month = now.getMonth() + 1;
				var date = now.getDate();

				// 星期天是0
				var week = now.getDay();

				var hour = now.getHours();
				var minute = now.getMinutes();

				var second = now.getSeconds();

				// 弹出瞬间的时间
				// alert(hour+'|'+minute+'|'+second);

				oDiv.innerHTML = '当前时间是:'+year+'年'+month+'月'+date+'日'+' '+toWeek(week)+' '+toDouble(hour)+':'+toDouble(minute)+':'+toDouble(second);
				}

			// 这里需要先运行函数,是为了规避开启定时器会卡顿一秒的现象
			timego();
			setInterval(timego,1000);
		}

		function toWeek(num){

			switch(num){
				case 0:
					return '星期天';break;
				case 1:
					return '星期一';break;
				case 2:
					return '星期二';break;
				case 3:
					return '星期三';break;
				case 4:
					return '星期四';break;
				case 5:
					return '星期五';break;
				case 6:
					return '星期六';break;
			}
		}

		function toDouble(num){
			if(num<10){
				return '0'+num;
			}
			else{
				return num;
			}
		}
	</script>
</head>
<body>
	<div id="div1"></div>
</body>
</html>